package com.victor.zjbj_android.yinhuan;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.victor.zjbj_android.R;
import com.victor.zjbj_android.bean.YhtypeListBean;
import com.victor.zjbj_android.utils.FastJsonTools;
import com.victor.zjbj_android.utils.MainUtils;
import com.victor.zjbj_android.utils.ZjbjManager;
import com.victor.zjbj_android.utils.retrofit.BaseSubscriber;
import com.victor.zjbj_android.utils.retrofit.ExceptionHandle;
import com.victor.zjbj_android.utils.retrofit.RetrofitUtils;
import com.victor.zjbj_android.yinhuan.adapter.MyExAdapter;
import com.victor.zjbj_android.bean.GroupBean;
import com.victor.zjbj_android.yinhuan.fragment.YihuanListFragment;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class YinHuanTiJiaoActivity extends AppCompatActivity implements OnDateSetListener{
    private TabLayout mTablayout;
    private ViewPager mViewPager;
    private List<Fragment> fragments;

    public ArrayList<YhtypeListBean.DataBean.DataItemBean> alYhtype;

    private YihuanListFragment myFragment1;
    private YihuanListFragment myFragment2;
    private YihuanListFragment myFragment3;
    private YihuanListFragment myFragment4;

    private void initFragments() {
        myFragment1 = YihuanListFragment.newInstance(0);
        myFragment2 = YihuanListFragment.newInstance(1);
        myFragment3 = YihuanListFragment.newInstance(2);
        myFragment4 = YihuanListFragment.newInstance(3);
        fragments = new ArrayList<>();
        fragments.add(myFragment1);
        fragments.add(myFragment2);
        fragments.add(myFragment3);
        fragments.add(myFragment4);
    }
    ArrayList<GroupBean> groupList ; //存储部门数据
    ArrayList<List> childList ; //存储装有人员信息的List
    private TextView tv_type;
    private TextView tv_time;
    ExpandableListView expandableListView;
    private RelativeLayout rl_type;
    SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
    TimePickerDialog   mDialogYearMonthDay;
    private RelativeLayout rl_starttime;
    private RelativeLayout rl_endtime;
    private TextView tv_starttime;
    private TextView tv_endtime;
    private RelativeLayout rl_time;
    private int timeType;
    private ImageView iv_add;
    private int typeState;
    private int timeState;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yin_huan_ti_jiao);
        typeState=0;
        timeState=0;
        alYhtype = new ArrayList<>();
        //填充数据
        initData();

    }

    private void createStep2()
    {
        initFragments();
        initView();
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });



        expandableListView = (ExpandableListView) findViewById(R.id.view_exlist);
        //new一个适配器，就是之前我们创建的适配器，并且填入参数
        MyExAdapter adapter = new MyExAdapter(this,//Activity自带Context属性，所以传this进去就行了
                R.layout.item_group_exlist,//我们之前创建的显示部门名称的布局
                R.layout.item_exlist, //我们之前创建的显示部门人员信息的布局
                groupList, //刚刚创建的装有部门数据的List对象
                childList); //刚刚创建的装有人员信息的List的List对象

        //设置适配器，传入刚刚new的adapter适配的对象
        expandableListView.setAdapter(adapter);
    }

    private void initView() {
        mDialogYearMonthDay = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH_DAY)
                .setCallBack(YinHuanTiJiaoActivity.this)
                .setThemeColor(getResources().getColor(R.color.colorPrimary))
                .setToolBarTextColor(getResources().getColor(R.color.colorPrimary))
                .setWheelItemTextSelectorColor(getResources().getColor(R.color.colorPrimary))
                .build();

        iv_add=findViewById(R.id.iv_add);
        rl_time=findViewById(R.id.rl_time);
        rl_starttime=findViewById(R.id.rl_starttime);
        rl_endtime=findViewById(R.id.rl_endtime);
        tv_starttime=findViewById(R.id.tv_starttime);
        tv_endtime=findViewById(R.id.tv_endtimetime);
        tv_type=findViewById(R.id.tv_type);
        tv_time=findViewById(R.id.tv_time);
        rl_type=findViewById(R.id.rl_type);
        mViewPager=findViewById(R.id.viewPager);
        mTablayout=findViewById(R.id.tb_wenjian);
        MyYinHuanPagerAdapter myWenjianPagerAdapter=new MyYinHuanPagerAdapter(getSupportFragmentManager(),fragments,new String[]{"全部","待确认","已确认","已驳回"});
        mViewPager.setAdapter(myWenjianPagerAdapter);
        mTablayout.setupWithViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        myFragment1.refresh(0);
                        break;
                    case 1:
                        myFragment2.refresh(1);
                        break;
                    case 2:
                        myFragment3.refresh(2);
                        break;
                    case 3:
                        myFragment4.refresh(3);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        tv_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (typeState==0){
                    rl_type.setVisibility(View.VISIBLE);
                    rl_time.setVisibility(View.GONE);
                    typeState=1;
                }else {
                    rl_type.setVisibility(View.GONE);
                    rl_time.setVisibility(View.GONE);
                    typeState=0;
                }



            }
        });
        tv_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(timeState==0){
                    rl_type.setVisibility(View.GONE);
                    rl_time.setVisibility(View.VISIBLE);
                    timeState=1;
                }else {
                    rl_type.setVisibility(View.GONE);
                    rl_time.setVisibility(View.GONE);
                    timeState=0;
                }


            }
        });

        rl_starttime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                timeType=0;
                mDialogYearMonthDay.show(getSupportFragmentManager(), "year_month_day");
            }
        });
        rl_endtime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                timeType=1;
                mDialogYearMonthDay.show(getSupportFragmentManager(), "year_month_day");
            }
        });
        iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(YinHuanTiJiaoActivity.this,NewYinhuanActivity.class));
            }
        });

    }
    public void initData() {
        //初始化变量
        groupList = new ArrayList();
        childList = new ArrayList();

        //添加部门信息
        groupList.add(new GroupBean("隐患类别"));


        RetrofitUtils mRetrofit1 = RetrofitUtils.getInstance(getApplicationContext()).createBaseApi();
        JSONObject jsonObject=new JSONObject();
        ZjbjManager tzsbManager=new ZjbjManager();

        tzsbManager.getYhtypeList(jsonObject.toString(), MainUtils.getToken(YinHuanTiJiaoActivity.this), mRetrofit1, new BaseSubscriber() {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {

            }

            @Override
            public void onSuccess(JSONObject jsonObject) throws JSONException {
                YhtypeListBean requestBean= FastJsonTools.getBean(jsonObject.toString(),YhtypeListBean.class);
                if(requestBean.getCode().equals("0")){
                    if (alYhtype.size() > 0) {
                        alYhtype.clear();
                    }
                    alYhtype = (ArrayList<YhtypeListBean.DataBean.DataItemBean>) requestBean.getData().getDataItem();
                    childList.add(alYhtype);
                }else {
                    ToastUtils.showShort(requestBean.getInfo());
                }
                createStep2();

            }
        });



    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        String text = getDateToString(millseconds);
        if(timeType==0){
            tv_starttime.setText(text);
        }else {
            tv_endtime.setText(text);
        }
        Log.e("11",text);
    }
    public String getDateToString(long time) {
        Date d = new Date(time);
        return sf.format(d);
    }
    /**
     * 强制隐藏键盘
     */
    public void forceHideIM() {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager == null) {
            return;
        }
        View decorView = getWindow().getDecorView();
        inputMethodManager.hideSoftInputFromWindow(decorView.getWindowToken(), 0);
    }
}
